from django.db import models

# Create your models here.


# 商品分类
class ClassLevel1(models.Model):
    name = models.CharField(max_length=100, verbose_name=u'名称')
    msg = models.CharField(max_length=300, verbose_name=u'描述',null=True, blank=True)
    img = models.CharField(max_length=300, verbose_name=u'图标')
    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = "商品一级分类"
class ClassLevel2(models.Model):
    name = models.CharField(max_length=100, verbose_name=u'名称')
    msg = models.CharField(max_length=300, verbose_name=u'描述',null=True, blank=True)
    img = models.CharField(max_length=300, verbose_name=u'图标')

    ClassID = models.ForeignKey('ClassLevel1',null=True, blank=True,on_delete=models.CASCADE,verbose_name=u'选择一级分类')
    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = "商品二级分类"
class ClassLevel3(models.Model):
    name = models.CharField(max_length=100, verbose_name=u'名称')
    msg = models.CharField(max_length=300, verbose_name=u'描述',null=True, blank=True)
    img = models.CharField(max_length=300, verbose_name=u'图标')

    ClassID = models.ForeignKey('ClassLevel2',null=True, blank=True,on_delete=models.CASCADE,verbose_name=u'选择二级分类')
    
    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = "商品三级分类"
#商品定位展示
class GoodsLocation1(models.Model):
    name = models.CharField(max_length=100, verbose_name=u'名称')
    msg = models.CharField(max_length=300, verbose_name=u'描述',null=True, blank=True)
    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = "一级定位"
class GoodsLocation2(models.Model):
    name = models.CharField(max_length=100, verbose_name=u'名称')
    msg = models.CharField(max_length=300, verbose_name=u'描述',null=True, blank=True)
    ClassID = models.ForeignKey('GoodsLocation1',null=True, blank=True,on_delete=models.CASCADE,verbose_name=u'选择一级定位')
    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = "二级定位"
class GoodsLocation3(models.Model):
    name = models.CharField(max_length=100, verbose_name=u'名称')
    msg = models.CharField(max_length=300, verbose_name=u'描述',null=True, blank=True)
    config = models.TextField(null=True, blank=True, verbose_name=u'自定义配置')
    ClassID = models.ForeignKey('GoodsLocation2',null=True, blank=True,on_delete=models.CASCADE,verbose_name=u'选择二级定位')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = "三级定位"
#商品集合
class GoodsGather(models.Model):
    name = models.CharField(max_length=300, verbose_name=u'名称')
    GoodsBase = models.OneToOneField(
        'GoodsLocation3', null=True, blank=True, on_delete=models.CASCADE, verbose_name=u'商品位置')
    goodsList = models.ManyToManyField('GoodsBase',verbose_name=u'商品列表') 

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = "商品集合"
# 商品信息
class GoodsBase(models.Model):
    title = models.CharField(max_length=300, verbose_name=u'名称')
    src = models.CharField(max_length=300, verbose_name=u'主图地址')
    star_time = models.DateTimeField(auto_now_add=True,verbose_name=u'创建日期')   
    last_time = models.DateTimeField(auto_now=True,verbose_name=u'最后一次修改日期')  
    isDelete = models.BooleanField(default=True,verbose_name=u'是否可用') 
  

    def __str__(self):
        return self.title

    def getPrice(self):
        data = GoodesDetailed.objects.get(GoodsBase=self.id).price
        return data

    def getDetailedObj(self):
        data = GoodesDetailed.objects.get(GoodsBase=self.id)
        return data
        
    class Meta:
        verbose_name_plural = "商品信息"

#商品详细
class GoodesDetailed(models.Model):   
    GENDER_CHOICES = (
        (u'1', u'上架'),
        (u'2', u'下架'),
    )
    title = models.CharField(max_length=300, verbose_name=u'描述')
    imgList = models.TextField(verbose_name=u'小图地址')
    richText =  models.TextField(verbose_name=u'图文介绍')
    price = models.FloatField(null=True, blank=True, verbose_name=u'价格')
    sales = models.IntegerField(null=True, blank=True, verbose_name=u'销量')
    inventory = models.IntegerField(null=True, blank=True, verbose_name=u'库存')
    star_time = models.DateTimeField(auto_now_add=True,verbose_name=u'创建日期')   
    last_time = models.DateTimeField(auto_now=True,verbose_name=u'最后一次修改日期')  
    shelves = models.CharField(
        max_length=30, verbose_name=u'上架状态', choices=GENDER_CHOICES)
    isDelete = models.BooleanField(default=True,verbose_name=u'是否可用')
    GoodsBase = models.OneToOneField(
        'GoodsBase', null=True, blank=True, on_delete=models.CASCADE, verbose_name=u'商品')
    goodsClass = models.ManyToManyField('ClassLevel3',verbose_name=u'分类') 
    Service = models.ManyToManyField('GoodesService',verbose_name=u'服务类型') 

    def __str__(self):
        return self.title
    class Meta:
        verbose_name_plural = "商品详细信息"

#服务类型
class GoodesService(models.Model):
    title = models.CharField(max_length=300, verbose_name=u'服务名称')
    msg = models.CharField(max_length=300,null=True, blank=True, verbose_name=u'备注')

    def __str__(self):
        return self.title

    class Meta:
        verbose_name_plural = "服务类型"

#颜色类
class ColorClass(models.Model):
    title = models.CharField(max_length=300, verbose_name=u'名称')
    def __str__(self):
        return self.title
    class Meta:
        verbose_name_plural = "颜色类"

#商品颜色
class GoodsColor(models.Model):
    title = models.CharField(max_length=300, verbose_name=u'名称')
    Goods = models.ForeignKey('GoodesDetailed',on_delete=models.CASCADE,verbose_name=u'商品')

    def __str__(self):
        return self.title
    class Meta:
        verbose_name_plural = "商品颜色"
#套餐
class specifications(models.Model):
    title = models.CharField(max_length=300, verbose_name=u'名称')
    Goods = models.ForeignKey('GoodesDetailed',on_delete=models.CASCADE,verbose_name=u'商品')
    def __str__(self):
        return self.title
    class Meta:
        verbose_name_plural = "套餐"
# 搭配  价格和库存
class Goodspackage(models.Model):
    colorID = models.ForeignKey(
        'GoodsColor',on_delete=models.CASCADE, verbose_name=u'颜色ID')
    speID = models.ForeignKey(
        'specifications',on_delete=models.CASCADE, verbose_name=u'套餐ID')
    price = models.FloatField(null=True, blank=True, verbose_name=u'价格')
    inventory = models.IntegerField(null=True, blank=True, verbose_name=u'库存')
    src = models.CharField(max_length=300, verbose_name=u'主图地址')

    Goods = models.ForeignKey('GoodesDetailed',null=True, blank=True,on_delete=models.CASCADE,verbose_name=u'商品')

    class Meta:
        verbose_name_plural = "套餐搭配"
